package com.nova.bluetooth.helpUtils;

import android.util.Log;

public class LogUtil {
    public static String tag = LogUtil.class.getSimpleName();

    public static int VERBOSE = 1;
    public static int DEBUG = 2;
    public static int INFO = 3;
    public static int WARN = 4;
    public static int ERROR = 5;
    public static int NOTHING = 6;

    public static int LEVEL = VERBOSE;//当前Log的打印等级
    private static boolean needDetail = true;//是否需要Log详细信息

    public static void v(String msg) {
        if (LEVEL <= VERBOSE) {
            Log.v(tag, getDetailInfo() + msg);
        }
    }

    public static void d(String msg) {
        if (LEVEL <= DEBUG) {
            Log.d(tag, getDetailInfo() + msg);
        }
    }

    public static void i(String msg) {
        if (LEVEL <= INFO) {
            Log.i(tag, getDetailInfo() + msg);
        }
    }

    public static void w(String msg) {
        if (LEVEL <= WARN) {
            Log.w(tag, getDetailInfo() + msg);
        }
    }

    public static void e(String msg) {
        if (LEVEL <= ERROR) {
            Log.e(tag, getDetailInfo() + msg);
        }
    }

    private static String getDetailInfo() {
        if (needDetail) {
            return getClassName() + "$" + getMethodName() + "(" + getLineNumber() + ")"+ ": ";
        }
        return "";
    }

    /**
     * 获取Log所在的类名
     * @return
     */
    private static String getClassName() {
        try {
            String classPath = Thread.currentThread().getStackTrace()[5].getClassName();
            return classPath.substring(classPath.lastIndexOf(".") + 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取Log所在的方法名
     * @return
     */
    private static String getMethodName() {
        try {
            return Thread.currentThread().getStackTrace()[5].getMethodName();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取Log所在的行
     * @return
     */
    private static int getLineNumber() {
        try {
            return Thread.currentThread().getStackTrace()[5].getLineNumber();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
